package com.bxshey.happyTown.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bxshey.happyTown.entity.CardRolls;
import com.bxshey.happyTown.entity.User;
import com.bxshey.happyTown.exception.BusinessException;
import com.bxshey.happyTown.mapper.CardRollsMapper;
import com.bxshey.happyTown.mapper.UserMapper;
import com.bxshey.happyTown.result.ResultEnum;
import com.bxshey.happyTown.service.CardRollsService;
import com.bxshey.happyTown.service.UserService;
import com.bxshey.happyTown.utils.MyMail;
import com.bxshey.happyTown.vo.cardRolls.CardRollsVO;
import com.bxshey.happyTown.vo.cardRolls.RecordingVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @作者：不想说话而已
 * @Date：2024/1/4 12:13
 */
@Service
public class CardRollsServiceImpl extends ServiceImpl<CardRollsMapper, CardRolls> implements CardRollsService {

    @Resource
    CardRollsMapper cardRollsMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    MyMail mail;

    /**
     *
     * @param uid 用户id
     * @param statue 卡卷状态
     * @return 多个卡卷信息
     */
    @Override
    public List<CardRollsVO> queryALL(Integer uid, Integer statue) {
        return cardRollsMapper.queryAll(uid,statue);
    }

    /**
     * 新增卡卷信息
     * @param uid 用户id
     * @param kind 卡卷类型
     * @return 返回积分余额
     */
    @Override
    public Integer insertOne(Integer uid, Integer kind) {
        User user = userMapper.queryById(uid);
        int nowIntegral = user.getIntegral() - 10;
        if(nowIntegral<0){
            throw new BusinessException(ResultEnum.ERROR.code,"积分不足");
        }
        user.setIntegral(nowIntegral);
        System.out.println(user);
        System.out.println(nowIntegral);
        userMapper.update(user);
        Calendar calendar = Calendar.getInstance();
        CardRolls cardRolls = new CardRolls();
        cardRolls.setCardNumber(UUID.randomUUID().toString());
        cardRolls.setCreateTime(calendar.getTime());
        cardRolls.setUid(uid);
        cardRolls.setKind(kind);
        calendar.add(Calendar.YEAR,1);
        cardRolls.setExpirationTime(calendar.getTime());
        cardRollsMapper.insertOne(cardRolls);
        return nowIntegral;
    }

    /**
     * 获取所有卡卷记录
     * @param uid 用户id
     * @return 所有卡卷记录
     */
    @Override
    public List<RecordingVO> recording(Integer uid) {
        return cardRollsMapper.recording(uid);
    }

    /**
     * 使用卡卷
     * @param uid 用户id
     * @param id  卡卷id
     * @return 返回成功数
     */
    @Override
    public Integer choose(Integer uid, Integer id) throws MessagingException {
        CardRollsVO cardRolls = cardRollsMapper.queryById(id);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String msg = "<h1 style='color: #ec3d80;font-weight: 600;'>申请人：春余呀!</h1>"
                + "<p style='color : rgb(243, 118, 205);font-weight: 600;'>申请时间："+format.format(new Date())
                + "</p><p style='color : rgb(243, 118, 205); font-weight: 600;'>获取时间："+cardRolls.getCreateTime()
                + "</p><p style='color : rgb(243, 118, 205);font-weight: 600;'>截止时间："+cardRolls.getExpirationTime()
                + "<p style='color : rgb(243, 118, 205);font-weight: 600;+'>物品信息："+cardRolls.getMsg()
                + "</p>";
        mail.sendSimpleMail("Baby的幸福小镇--奖励申请","905102975@qq.com", msg,true);
        return  cardRollsMapper.choose(uid,id,2);
    }
}
